//
//  UIViewController+Category.m
//  jujing
//
//  Created by xiatiejun on 2021/9/8.
//

#import "UIViewController+Category.h"

static const NSInteger activityIndicatorViewTag = 100110;

@implementation UIViewController (Category)

- (NSArray *)removeNavChildViewControllers:(NSArray <NSString *>*)controllers {

    if (!self.navigationController.viewControllers.count) return nil;
    if (!controllers.count) return self.navigationController.viewControllers;

    NSMutableArray *array = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [controllers enumerateObjectsUsingBlock:^(NSString *targetController, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([NSStringFromClass([obj class]) isEqualToString:targetController]) {
                [array removeObject:obj];
            }
        }];
    }];
    return array.copy;
}


- (void)dismissToRootViewController {
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
}


- (void)dismissToRootViewControllerWithAnimated:(BOOL)animated {
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:animated completion:nil];
}

- (void)dismissToRootViewController:(BOOL)animated completion: (void (^ __nullable)(void))completion {
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:animated completion:completion];
}

- (void)showLoading {
    [self showLoadingOffsetCenterY:0];
}

- (void)showLoadingOffsetCenterY:(CGFloat)centerY {

    UIActivityIndicatorViewStyle style = UIActivityIndicatorViewStyleGray;
    if (@available(iOS 13.0, *)) {
        style = UIActivityIndicatorViewStyleMedium;
    }

    UIActivityIndicatorView *_activityIndicatorView = [self.view viewWithTag:activityIndicatorViewTag];
    if (_activityIndicatorView) {
        [_activityIndicatorView removeFromSuperview];
    }

    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
    [self.view addSubview:activityIndicatorView];
    activityIndicatorView.hidesWhenStopped = YES;
    activityIndicatorView.tag = activityIndicatorViewTag;
    activityIndicatorView.transform = CGAffineTransformMakeScale(1.15, 1.15);
    [activityIndicatorView startAnimating];
    [activityIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.offset(centerY);
    }];
}

- (BOOL)isAnimation {
    UIActivityIndicatorView *_activityIndicatorView = [self.view viewWithTag:activityIndicatorViewTag];
    return [_activityIndicatorView isAnimating];
}

- (void)hiddenLoading {
    dispatch_safe_queue(^{
        UIActivityIndicatorView *activityIndicatorView = [self.view viewWithTag:activityIndicatorViewTag];
        if (activityIndicatorView) {
            [activityIndicatorView stopAnimating];
            [activityIndicatorView removeFromSuperview];
        }
    });

}

- (void)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message leftButtonName:(nullable NSString *)leftButtonName rightButtonName:(nullable NSString *)rightButtonName leftButtonBlock:(nullable void (^)(void))leftButtonBlock rightButtonBlock:(nullable void (^)(void))rightButtonBlock {

    if (![message isKindOfClass:NSString.class]) message = nil;
    if (![title isKindOfClass:NSString.class]) title = nil;

    if (!message.length && !title.length) return;
//
//    void (^leftActionBlock)(GJAlertAction *action) = nil;
//    void (^rightActionBlock)(GJAlertAction *action) = nil;
//
//    if (leftButtonName) {
//        leftActionBlock = ^(GJAlertAction *action){
//            action.title = leftButtonName;
//            action.titleColor = UIColor.tj_101320Color;
//            action.handler = leftButtonBlock;
//         };
//    }
//
//    if (rightButtonName) {
//        rightActionBlock = ^(GJAlertAction *action){
//            action.title = rightButtonName;
//            action.titleColor = UIColor.tj_blueColor;
//            action.handler = rightButtonBlock;
//         };
//    }
//
//    [GJAlertController alertInController:self title:title message:message left:leftActionBlock right:rightActionBlock];
}


@end
